﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using UserAccountModule.Models;
using UserAccountModule.Services;
using ServiceAgent;
using ServiceAgent.CatalogueService;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using UserAccountModule.Views;
using Infrastructure.Account;
using ServiceAgent.UserAccountService;
using AutoMapper;

namespace UserAccountModule.ViewModels
{
    /// <summary>
    /// ViewModel for EditAccountView.
    /// </summary>
    public class EditAccountViewModel : NotificationObject, INavigationAware
    {
        #region Fields
        private readonly IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        int uid;

        UserDTO user;

        string password;
        string newPassword;
        string reNewPassword;



        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public EditAccountViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;

            this.Initialize();
        }
        #endregion

        #region Administrative Properties
        public UserDTO User
        {
            get { return user; }
            set
            {
                user = value;
                RaisePropertyChanged(() => this.User);
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                RaisePropertyChanged(() => this.Password);
            }
        }

        public string NewPassword
        {
            get { return newPassword; }
            set
            {
                newPassword = value;
                RaisePropertyChanged(() => this.NewPassword);
            }
        }

        public string ReNewPassword
        {
            get { return reNewPassword; }
            set
            {
                reNewPassword = value;
                RaisePropertyChanged(() => this.ReNewPassword);
            }
        }
        #endregion

        #region Command Properties
        public ICommand SaveCommand { get; private set; }
        public ICommand ChangePasswordCommand { get; private set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.SaveCommand = new DelegateCommand(this.OnRaiseSave);
            this.ChangePasswordCommand = new DelegateCommand(this.OnRaiseChangePassword);
        }

        void GetUser(int id)
        {
            try
            {
                using (var proxy = container.Resolve<UserAccountServiceAgent>())
                {
                    //Mapper.CreateMap<UserDTO, User>();
                    //User = Mapper.Map<UserDTO, User>(proxy.GetUserByID(id));
                    User = proxy.GetUserByID(id);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSave()
        {
            try
            {
                using (var proxy = container.Resolve<UserAccountServiceAgent>())
                {
                    //Mapper.CreateMap<User, UserDTO>();
                    //proxy.UpdateUserAccount(Mapper.Map<User, UserDTO>(User));
                    proxy.UpdateUserAccount(User);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseChangePassword()
        {

            if (Password == User.MatKhau)
            {
                if (NewPassword == ReNewPassword)
                {
                    try
                    {
                        using (var proxy = container.Resolve<UserAccountServiceAgent>())
                        {
                            proxy.ChangePassword(User.ID, NewPassword);
                            MessageBox.Show("Lưu thành công");
                        }
                    }
                    catch (Exception) { MessageBox.Show("Có lỗi xảy ra"); }

                }
            }
            else { MessageBox.Show("Mật khẩu không chính xác"); }
        }

        private bool CanChangePassword()
        {
            if (this.NewPassword != null && this.ReNewPassword != null && this.Password != null)
            {
                return true;
            }
            return false;
        }

        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            string id = navigationContext.Parameters["ID"];
            if (!string.IsNullOrEmpty(id))
            {
                this.uid = Convert.ToInt32(id);
            }
            this.GetUser(uid);
        }
        #endregion
    }
}
